package com.kaisu.blog.web.user.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ShearCaptcha;
import com.kaisu.blog.application.user.dto.LoginDTO;
import com.kaisu.blog.application.user.service.UserService;
import com.kaisu.blog.web.result.Result;
import com.kaisu.blog.web.user.convert.UserWebConvert;
import com.kaisu.blog.web.user.request.*;
import com.kaisu.blog.web.user.vo.FollowVO;
import com.kaisu.blog.web.user.vo.LoginVO;
import com.kaisu.blog.web.user.vo.UserVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author kaisu
 */
@Tag(name = "user", description = "用户")
@RestController
@RequestMapping("/api/v1/user")
@RequiredArgsConstructor
public class UserController {
    private final UserService userService;
    private final UserWebConvert userConvert;

    @PostMapping("login")
    @Operation(summary = "登录")
    public Result<LoginVO> userLogin(HttpSession session, @RequestBody LoginRequest request) {
        String verifyCode = (String) session.getAttribute(getVerifyCode());
        /*if (Objects.isNull(verifyCode) || Objects.isNull(request.verifyCode()) || !verifyCode.equalsIgnoreCase(request.verifyCode())) {
            return Result.fail(ResultCode.FAIL.getCode(), "验证码错误！");
        }*/
        LoginDTO loginDTO = userService.login(request.account(), request.password());
        LoginVO loginVO = userConvert.toVO(loginDTO);
        return Result.success(loginVO);
    }

    @PostMapping("register")
    @Operation(summary = "注册")
    public void userRegister(@RequestBody UserCreateRequest request) {
        userService.create(userConvert.toDTO(request));
    }

    @PostMapping("update")
    @Operation(summary = "更新用户信息")
    public void userUpdate(@RequestBody UserUpdateRequest request) {
        userService.update(userConvert.toDTO(request));
    }

    @PostMapping("update/avatar")
    @Operation(summary = "更新用户头像")
    public void updateUserAvatar(@RequestBody UserUpdateAvatarRequest request) {
        userService.updateAvatar(request.fileId());
    }

    @PostMapping("update/password")
    @Operation(summary = "修改密码")
    public void userUpdatePassword(@RequestBody UserUpdatePasswordRequest request) {
        userService.updatePassword(request.password(), request.newPassword());
    }

    @GetMapping("currentUser")
    @Operation(summary = "获取当前用户")
    public Result<UserVO> currentUser() {
        UserVO userVO = userConvert.toVO(userService.currentUser());
        return Result.success(userVO);
    }

    @GetMapping("/code")
    @Operation(summary = "用户登录所需验证码")
    public void userLoginVerifyCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(150, 40, 5, 4);
        captcha.write(response.getOutputStream());
        request.getSession().setAttribute(getVerifyCode(), captcha.getCode());
    }

    private static String getVerifyCode() {
        return "verifyCode";
    }

    @Operation(summary = "关注")
    @PostMapping("follow/{followingUserId}")
    public void userFollow(@PathVariable String followingUserId) {
        userService.follow(followingUserId);
    }

    @Operation(summary = "取消关注")
    @PostMapping("unfollow/{followingUserId}")
    public void userUnfollow(@PathVariable String followingUserId) {
        userService.unfollow(followingUserId);
    }

    @Operation(summary = "粉丝列表")
    @GetMapping("followers")
    public Result<Set<FollowVO>> userFollowers() {
        return Result.success(userService.followers().stream().map(userConvert::toVO).collect(Collectors.toSet()));
    }

    @Operation(summary = "关注列表")
    @GetMapping("followings")
    public Result<Set<FollowVO>> userFollowings(@RequestBody UserFollowersRequest request) {
        return Result.success(userService.followings(request.getKey()).stream().map(userConvert::toVO).collect(Collectors.toSet()));
    }
}
